#include <Windows.h>
#include <gl\GL.h>
#include <gl\GLU.h>
#include <gl\glut.h>

#include "gameobj.h"


gameWorld newWorld;
float rotates = 15;
vec3 lookat(0,0,0);
vec3 lightpos(2, 2, 2);
float h_angle = 30, w_angle = 0, dis = 5;
float camSpeed = 0.5;
float strength = 0, hitAngle = 0 ;

enum CamProfile { FREE, BALL, TOP };

CamProfile profile = BALL;

void lightDis(GLenum id, vec3 pos)
{
	GLfloat position[] = {pos.vx, pos.vy, pos.vz, 1.0};
	//GLfloat diffuse[] = {0.5, 0.5, 0.5, 
	glLightfv(id, GL_POSITION, position);

	glDisable(GL_LIGHTING);
	glPushMatrix();
	glTranslatef(pos.vx, pos.vy, pos.vz);
	glColor3f(1,1,1);
	glutSolidSphere(0.2, 20, 20);
	glPopMatrix();
	
}

void cb_display()
{
	glMatrixMode( GL_MODELVIEW );		// Setup model transformations
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glLoadIdentity();

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	
	switch(profile)
	{
		case FREE:
			newWorld.camera(dis, h_angle, w_angle, lookat);
			break;
		case BALL:
			newWorld.camera(dis, h_angle, w_angle, newWorld.ballLoc->location);
			break;
		case TOP:
			newWorld.camera(dis, 90, w_angle, lookat);
			break;
	}
	
	//glTranslatef(0, 0, -10);
	//glRotatef(rotates, 1, 0, 0);

	newWorld.update(1);

	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);

	lightDis(GL_LIGHT0, lightpos);

	newWorld.draw();
	//rotates+=5;
	//cout << "dra11w!" << endl;

	
	glFlush();
	glutSwapBuffers();
}

void cb_idle()
{
	//glMatrixMode(GL_PROJECTION

	//newWorld.update(1);

	//rotates+=1;
	//camX +=1;

	//cout << camX << endl;
	//cout << "update!" << endl;
	 
	//gluLookAt(20, 15, 15, 1, 0, 0, 0, 1, 0);
	
	//gluLookAt(camX, 15, 0, 0, 0, 0, 0,1,0);

	//glutPostRedisplay();
}

void cb_keyboard(unsigned char key, int x, int y)
{
	switch(key)
	{
		case 'a':
			w_angle--;
			if(w_angle < -180)
				w_angle = 180;
			break;
		case 'd':
			w_angle++;
			if(w_angle > 180)
				w_angle = -180;
			break;
		case 'W':
			h_angle++;
			if(h_angle > 90)
				h_angle = -90;
			break;
		case 'S':
			h_angle--;
			if(h_angle < -90)
				h_angle = 90;
			break;
		case 'w':
			dis-=camSpeed;
			break;
		case 's':
			dis+=camSpeed;
			break;


	}
}

void cb_specialKeys(int key, int x, int y)
{
	switch(key)
	{
		case GLUT_KEY_UP:
			newWorld.ballLoc->direction.vx+= 0.1;
			break;
		case GLUT_KEY_DOWN:
			newWorld.ballLoc->direction.vx-= 0.1;
			break;
		case GLUT_KEY_RIGHT:
			newWorld.ballLoc->direction.vz+= 0.1;
			break;
		case GLUT_KEY_LEFT:
			newWorld.ballLoc->direction.vz-= 0.1;
			break;
	}
}

void subMenu(int id)
{
	switch(id)
	{
		case 0:
			profile = FREE;
		break;
		case 1:
			profile = BALL;
		break;
		case 2:
			profile = TOP;
		break;
	}
}

void menu(int id)
{
}

void menuMaker() //putting menu code here for main to stay concise
{
	int c_menu = glutCreateMenu(subMenu);
	glutAddMenuEntry("FREE", 0);
	glutAddMenuEntry("BALL", 1);
	glutAddMenuEntry("TOP", 2);

	glutCreateMenu(menu);
	glutAddSubMenu("Camera Profile", c_menu);

}

int main(int argc, char **argv)
{
	newWorld.createLevel(argv[1]);
	//newWorld.createLevel(argv[2]);	
	newWorld.loadLevel(argv[1]);	

	glutInit(&argc, argv);
	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
	glutInitWindowSize(800,600);
	glutCreateWindow("MGolfEngine");

	/*callback functions go here */
	glutIdleFunc(cb_display);
	glutDisplayFunc(cb_display);
	glutKeyboardFunc(cb_keyboard);
	glutSpecialFunc(cb_specialKeys);

	/*callback ends*/

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(70, 16/9, 1, 1500);

	//glOrtho(1, 1, 50, 50, 1, 500);

	//glMatrixMode(GL_MODELVIEW); //Setup model transforms
	//glLoadIdentity();
	menuMaker();
	glutAttachMenu(GLUT_RIGHT_BUTTON);

	glClearDepth(1.0);
	glClearColor(0,0,0,0);
	glEnable(GL_DEPTH_TEST);

	glutMainLoop();


	return 0;
}